package com.fanghaoran.view.user.bookFrame;

import com.fanghaoran.controller.InfoController;
import com.fanghaoran.controller.WordController;
import com.fanghaoran.entity.Users;
import com.fanghaoran.view.LoginFrame;
import com.fanghaoran.view.user.BackGroundPanel;
import com.fanghaoran.entity.Words;
import com.fanghaoran.view.user.studyPlanFrame.StudyPlanPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * @Author: win11
 * @Date: 2023/03/19/1:06
 * @Description: 显示答案的页面
 */
public class AnswerFrame extends JFrame {

    /**
     * 用户的个人信息
     */
    private final Users user=(Users)new InfoController().getInfo(LoginFrame.usernameInput).getMessage();

    public AnswerFrame(Words word) throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView(word);
        //打开页面执行的操作
        open();
        //设置可见
        this.setVisible(true);
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    private void initView(Words word) throws IOException {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //垂直box存放组件实现垂直排列
        Box vBox=Box.createVerticalBox();
        vBox.setBounds(50,50,100,100);
        vBox.add(Box.createVerticalStrut(100));

        //单词图标
        JLabel wordLabel =new JLabel(word.getWord());

        //中文释义
        JLabel translation=new JLabel(word.getTranslation(),SwingConstants.CENTER);

        //水平box实现两个按钮水平排列
        Box hBox=Box.createHorizontalBox();
        //按钮
        JButton returnKey=new JButton("返回");
        JButton next=new JButton("下一个");
        //按钮添加到box
        hBox.add(returnKey);
        hBox.add(next);

        Box box=Box.createHorizontalBox();
        JButton addToNote=new JButton("加入生词本");
        box.add(addToNote);

        //添加到垂直box
        vBox.add(wordLabel);
        vBox.add(translation);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(hBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box);
        //添加到panel
        panel.add(vBox);
        //添加到frame
        this.add(panel);

        //添加点击事件
        //返回键
        returnKey.addActionListener(e-> {
                try {
                    //关闭当前页面
                    exit();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        );
        //下一个
        next.addActionListener(e->{
            //随机数实现随机单词的学习
                Random r=new Random();
                int randomIndex = r.nextInt(ModelFrame.words.size());
                //关闭当前页面
                exit();
                try {
                    //跳转到新的单词学习页面
                    if(ModelFrame.model==ModelFrame.ENGLISH_TO_CHINESE) {
                        new TestFrame(ModelFrame.words.get(randomIndex));
                    }else if(ModelFrame.model==ModelFrame.CHINESE_TO_ENGLISH){
                        new TestFrame2(ModelFrame.words.get(randomIndex));
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        );
        //加入生词本
        addToNote.addActionListener(e-> {
                    try {
                        new WordController().updateNotedBook(word,user.getId());
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
            JOptionPane.showMessageDialog(panel,"添加成功");
                }
        );

    }

    /**
     * 关闭当前页面的方法
     */
    private void exit(){
        this.dispose();
    }

    /**
     * 开启答案页面执行的操作(已学单词数+1)
     */
    public void open(){
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                super.windowOpened(e);
                StudyPlanPanel.wordAmount++;
            }
        });
    }
}
